# Python学习展示项目 - Makefile
# 简化常用开发任务的执行

# ==========================================
# 变量定义
# ==========================================
PYTHON := python3
PIP := pip3
VENV_NAME := venv
VENV_PATH := ./$(VENV_NAME)
VENV_PYTHON := $(VENV_PATH)/bin/python
VENV_PIP := $(VENV_PATH)/bin/pip

# 项目目录
SRC_DIR := src
TEST_DIR := tests
DOCS_DIR := docs

# 代码质量工具
BLACK := black
ISORT := isort
FLAKE8 := flake8
MYPY := mypy
PYTEST := pytest
BANDIT := bandit
SAFETY := safety

# ==========================================
# 默认目标
# ==========================================
.PHONY: help
help: ## 显示帮助信息
	@echo "Python学习展示项目 - 可用命令:"
	@echo ""
	@echo "环境管理:"
	@echo "  setup          - 完整项目设置（创建虚拟环境并安装依赖）"
	@echo "  venv           - 创建虚拟环境"
	@echo "  install        - 安装生产依赖"
	@echo "  install-dev    - 安装开发依赖"
	@echo "  update         - 更新所有依赖"
	@echo "  freeze         - 生成当前依赖列表"
	@echo "  clean          - 清理临时文件和缓存"
	@echo ""
	@echo "代码质量:"
	@echo "  format         - 格式化代码（black + isort）"
	@echo "  lint           - 代码检查（flake8 + mypy）"
	@echo "  security       - 安全检查（bandit + safety）"
	@echo "  check          - 完整代码质量检查"
	@echo ""
	@echo "测试:"
	@echo "  test           - 运行所有测试"
	@echo "  test-unit      - 运行单元测试"
	@echo "  test-cov       - 运行测试并生成覆盖率报告"
	@echo "  test-watch     - 监控模式运行测试"
	@echo ""
	@echo "开发服务:"
	@echo "  run            - 启动FastAPI开发服务器"
	@echo "  run-prod       - 启动生产服务器"
	@echo "  jupyter        - 启动Jupyter Notebook"
	@echo "  shell          - 启动Python交互式shell"
	@echo ""
	@echo "文档:"
	@echo "  docs           - 生成文档"
	@echo "  docs-serve     - 启动文档服务器"
	@echo ""
	@echo "部署:"
	@echo "  docker-build   - 构建Docker镜像"
	@echo "  docker-run     - 运行Docker容器"
	@echo ""
	@echo "工具:"
	@echo "  deps-tree      - 显示依赖树"
	@echo "  deps-check     - 检查依赖冲突"
	@echo "  profile        - 性能分析"
	@echo ""

# ==========================================
# 环境管理
# ==========================================
.PHONY: setup
setup: venv install-dev ## 完整项目设置
	@echo "✅ 项目设置完成！"
	@echo "激活虚拟环境: source $(VENV_PATH)/bin/activate"

.PHONY: venv
venv: ## 创建虚拟环境
	@if [ ! -d "$(VENV_PATH)" ]; then \
		echo "🔧 创建虚拟环境..."; \
		$(PYTHON) -m venv $(VENV_PATH); \
		echo "✅ 虚拟环境创建完成"; \
	else \
		echo "ℹ️  虚拟环境已存在"; \
	fi

.PHONY: install
install: venv ## 安装生产依赖
	@echo "📦 安装生产依赖..."
	$(VENV_PIP) install --upgrade pip setuptools wheel
	$(VENV_PIP) install -r requirements.txt
	@echo "✅ 生产依赖安装完成"

.PHONY: install-dev
install-dev: venv ## 安装开发依赖
	@echo "📦 安装开发依赖..."
	$(VENV_PIP) install --upgrade pip setuptools wheel
	$(VENV_PIP) install -r requirements-dev.txt
	@echo "✅ 开发依赖安装完成"

.PHONY: update
update: ## 更新所有依赖
	@echo "🔄 更新依赖包..."
	$(VENV_PIP) install --upgrade -r requirements-dev.txt
	@echo "✅ 依赖更新完成"

.PHONY: freeze
freeze: ## 生成当前依赖列表
	@echo "📋 生成当前依赖列表..."
	$(VENV_PIP) freeze > requirements-current.txt
	@echo "✅ 依赖列表已保存到 requirements-current.txt"

.PHONY: clean
clean: ## 清理临时文件和缓存
	@echo "🧹 清理临时文件..."
	find . -type f -name "*.pyc" -delete
	find . -type d -name "__pycache__" -delete
	find . -type d -name "*.egg-info" -exec rm -rf {} +
	find . -type f -name ".coverage" -delete
	rm -rf .pytest_cache
	rm -rf .mypy_cache
	rm -rf htmlcov
	rm -rf dist
	rm -rf build
	@echo "✅ 清理完成"

# ==========================================
# 代码质量
# ==========================================
.PHONY: format
format: ## 格式化代码
	@echo "🎨 格式化代码..."
	$(VENV_PYTHON) -m $(BLACK) .
	$(VENV_PYTHON) -m $(ISORT) .
	@echo "✅ 代码格式化完成"

.PHONY: lint
lint: ## 代码检查
	@echo "🔍 代码检查..."
	$(VENV_PYTHON) -m $(FLAKE8) .
	$(VENV_PYTHON) -m $(MYPY) .
	@echo "✅ 代码检查完成"

.PHONY: security
security: ## 安全检查
	@echo "🔒 安全检查..."
	$(VENV_PYTHON) -m $(BANDIT) -r .
	$(VENV_PYTHON) -m $(SAFETY) check
	@echo "✅ 安全检查完成"

.PHONY: check
check: format lint security ## 完整代码质量检查
	@echo "✅ 所有检查完成"

# ==========================================
# 测试
# ==========================================
.PHONY: test
test: ## 运行所有测试
	@echo "🧪 运行测试..."
	$(VENV_PYTHON) -m $(PYTEST) $(TEST_DIR)

.PHONY: test-unit
test-unit: ## 运行单元测试
	@echo "🧪 运行单元测试..."
	$(VENV_PYTHON) -m $(PYTEST) $(TEST_DIR)/unit

.PHONY: test-cov
test-cov: ## 运行测试并生成覆盖率报告
	@echo "🧪 运行测试并生成覆盖率报告..."
	$(VENV_PYTHON) -m $(PYTEST) --cov=. --cov-report=html --cov-report=term
	@echo "📊 覆盖率报告已生成到 htmlcov/index.html"

.PHONY: test-watch
test-watch: ## 监控模式运行测试
	@echo "👀 监控模式运行测试..."
	$(VENV_PYTHON) -m $(PYTEST) -f $(TEST_DIR)

# ==========================================
# 开发服务
# ==========================================
.PHONY: run
run: ## 启动FastAPI开发服务器
	@echo "🚀 启动开发服务器..."
	$(VENV_PYTHON) -m uvicorn web_framework_comparison.01_fastapi_basic:app --reload --host 0.0.0.0 --port 8000

.PHONY: run-prod
run-prod: ## 启动生产服务器
	@echo "🚀 启动生产服务器..."
	$(VENV_PYTHON) -m gunicorn web_framework_comparison.01_fastapi_basic:app -w 4 -k uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000

.PHONY: jupyter
jupyter: ## 启动Jupyter Notebook
	@echo "📓 启动Jupyter Notebook..."
	$(VENV_PYTHON) -m jupyter notebook

.PHONY: shell
shell: ## 启动Python交互式shell
	@echo "🐍 启动Python shell..."
	$(VENV_PYTHON) -m IPython

# ==========================================
# 文档
# ==========================================
.PHONY: docs
docs: ## 生成文档
	@echo "📚 生成文档..."
	@if [ -d "$(DOCS_DIR)" ]; then \
		cd $(DOCS_DIR) && $(VENV_PYTHON) -m sphinx.cmd.build -b html . _build/html; \
	else \
		echo "⚠️  docs目录不存在，跳过文档生成"; \
	fi

.PHONY: docs-serve
docs-serve: ## 启动文档服务器
	@echo "📚 启动文档服务器..."
	@if [ -d "$(DOCS_DIR)/_build/html" ]; then \
		cd $(DOCS_DIR)/_build/html && $(PYTHON) -m http.server 8080; \
	else \
		echo "⚠️  请先运行 make docs 生成文档"; \
	fi

# ==========================================
# 部署
# ==========================================
.PHONY: docker-build
docker-build: ## 构建Docker镜像
	@echo "🐳 构建Docker镜像..."
	docker build -t python-learn-show .

.PHONY: docker-run
docker-run: ## 运行Docker容器
	@echo "🐳 运行Docker容器..."
	docker run -p 8000:8000 python-learn-show

# ==========================================
# 工具
# ==========================================
.PHONY: deps-tree
deps-tree: ## 显示依赖树
	@echo "🌳 显示依赖树..."
	$(VENV_PYTHON) -m pipdeptree

.PHONY: deps-check
deps-check: ## 检查依赖冲突
	@echo "🔍 检查依赖冲突..."
	$(VENV_PIP) check
	$(VENV_PYTHON) -m pipdeptree --warn conflict

.PHONY: profile
profile: ## 性能分析
	@echo "📊 性能分析..."
	@echo "请指定要分析的Python文件，例如: make profile FILE=your_script.py"
	@if [ -n "$(FILE)" ]; then \
		$(VENV_PYTHON) -m cProfile -o profile.stats $(FILE); \
		echo "性能分析结果已保存到 profile.stats"; \
	fi

# ==========================================
# 特殊目标
# ==========================================
.PHONY: all
all: clean setup check test ## 完整的CI/CD流程
	@echo "🎉 所有任务完成！"

# 确保虚拟环境存在的依赖
$(VENV_PATH)/bin/activate: requirements-dev.txt
	$(MAKE) install-dev

# 防止文件名冲突
.PHONY: requirements.txt requirements-dev.txt